var tableIns;

layui.extend().use(['jquery', 'layer', 'form', 'table', 'util'], function () {
    var $ = layui.jquery,
        layer = layui.layer,
        form = layui.form,
        table = layui.table,
        util = layui.util;

    var url;
    var mainIndex;

    $("#beginrk").click(function (e) {
        mainIndex = layer.open({
            content: $("#type-content"),
            type: 1,
            title: '入库准备',
            area: ['600px', '400px']
        });

        $("#sure_button").click(function () {
            layer.close(mainIndex);

            var upplier_select = $("#upplier_select option:selected");
            var skull_select = $("#skull_select option:selected");
            var warhuose_select = $("#warhuose_select option:selected");

            var a1 = upplier_select.val();
            $("#supplierId").val(a1);
            var a2 = skull_select.val();
            $("#skullId").val(a2);
            var a3 = warhuose_select.val();
            $("#warehouseId").val(a3);

            $("#text_upplier").val(upplier_select.text());
            $("#text_skull").val(skull_select.text());
            $("#text_warehuose").val(warhuose_select.text());
        });
    });

    $("#tianjia").click(function () {
        var date = new Date();
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var day = date.getDate();
        var d = year + "-" + month + "-" + day;

        var dateAStr = $("input[name=produceDate]").val();
        var produceDate = new Date(dateAStr);
        var dateAT = produceDate.getTime();

        var dateBStr = $("input[name=validDate]").val();
        var valiDate = new Date(dateBStr);
        var dateBT = valiDate.getTime();

        var drugstorenum = $("#drugstorenum").val(); // 入库数量
        var tardePrice = $("#tradePrice").val(); // 批发价
        var drugstoreName = $("#drugstoreName").val(); // 药品名称
        var produceDate = $("#produceDate").val(); // 生产日期
        var validDate = $("#validDate").val(); // 有效日期

        if (drugstoreName == '' || drugstoreName == null) {
            layer.msg("请选择入库的药品！");
            return false;
        } else if (drugstorenum <= 0) {
            layer.msg("请输入正确的入库数量！");
            return false;
        } else if (tardePrice <= 0) {
            layer.msg("请输入批发价格！");
            return false;
        } else if (produceDate === valiDate) {
            layer.msg("请选择生产日期和有效日期！");
            return false;
        } else if (Date.parse(produceDate) >= Date.parse(valiDate)) {
            layer.msg("生产日期不能大于或等于有效日期！");
            return false;
        } else if (Date.parse(valiDate) <= Date.parse(d)) {
            layer.msg("有效日期不能为今天或今天前！");
            return false;
        } else if (Date.parse(produceDate) >= Date.parse(d)) {
            layer.msg("生产日期不能为今天或今天以后！");
            return false;
        } else {
            var batch = $("#batch").val();
            var warehouseId = $("#warehouseId").val();
            var params = $("#form_drug").serialize();
            $.ajax({
                url: '/warehouse/addDrugs',
                data: params+ "&drugstoreName="+drugstoreName+
                    "&jiluname=" + drugstoreName + "&jilutype=常规入库"
                    + "&jilupihao=" + $("#batch").val()
                    + "&jilunumber=" + drugstorenum+"&skullId="+$("#skullId").val()+
                    "&warehouse="+$("#warehouseId").val(),
                dataType: 'JSON',
                type: 'post',
                success: function (data) {
                    if (data > 0) {
                        $("#form_drug")[0].reset();
                        layui.form.render();
                        setTimeout(function () {
                            layer.msg("添加成功！");
                        }, 1000);
                    } else {
                        layer.msg("添加失败！");
                    }
                }
            });
        }
    });

    $("#seldrugdty").click(function () {
        $("#div_table").hide();
        $("#chaxunkuang").show();
        tableIns = table.render({
            elem: '#userTable',
            url: '/warehouse/selectdgty',
            title: '库房药品表',
            cellMinWidth: 150,
            page: 1,
            limits: [10, 20, 30],
            limit: 10,
            cols: [[
                {field: 'drugId', title: '序号', width: '13%', align: 'center'},
                {field: 'drugName', title: '名称', width: '13%', align: 'center'},
                {field: 'unitName', title: '单位', width: '13%', align: 'center'},
                {field: 'sellingPrice', title: '售价', width: '13%', align: 'center'},
                {field: 'typeName', title: '类型', width: '13%', align: 'center'},
                {field: 'areaName', title: '产地', width: '13%', align: 'center'},
                {fixed: 'right', toolbar: '#userBar', title: '操作', width: '17%', align: 'center'},
            ]]
        });

        $("#doSearch").click(function () {
            var params = $("#searchFrm").serialize();
            tableIns.reload({
                url: "/warehouse/selectdgty?" + params,
            });
        });
    });

    table.on('tool(userTable)', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent == 'edit') {
            var supplier = $("#text_upplier").val();
            if (supplier == '') {
                layer.msg("请先准备入库信息！");
            } else {
                layer.confirm('确定添加【' + data.drugName + '】', function (index) {
                    var drugstoreName = data.drugName;
                    $("#drugstoreName").val(drugstoreName);
                    $("#sellingPrice").val(data.sellingPrice);
                    $("#unit").val(data.unitId);
                    $("#area").val(data.areaId);
                    $("#type").val(data.typeId);

                    $.ajax({
                        type: 'post',
                        url: '/warehouse/selnumber',
                        data: {
                            drugstoreName: drugstoreName,
                            warehouseId: $("#warehouseId").val()
                        },
                        dataType: 'JSON',
                        success: function (res) {
                            $("#batch").val(res.medicineid);
                            $("#dqkucun").val(res.number + data.unitName);
                        }
                    });

                    layer.closeAll('dialog');
                });
            }
        }
    });
});

